#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> work_type;
#define sla_time(a) (a.first)
#define score(a) (a.second)

struct comp {
    bool operator() (work_type& a, work_type& b) {
        if (sla_time(a) < sla_time(b)) {
            return true;
        } else if (sla_time(a) == sla_time(b) && (score(a) > score(b))) {
            return true;
        } else {
            return false;
        }
    }
};

int main() {
    // parse input 
    int work_nums;
    cin >> work_nums;
    // cout << work_nums << endl;
    vector<work_type> work_vectors(work_nums);
    int tmp;
    for (int i = 0; i < work_nums; ++i) {
        cin >> tmp;
        work_vectors[i].first = tmp;
        // cout << tmp << " ";
        cin >> tmp;
        work_vectors[i].second = tmp;
        // cout << tmp << " ";
    }

    sort(work_vectors.begin(), work_vectors.end(), comp());

    int cur_time = 0;
    int max_score = 0;
    // for (auto work:work_vectors) {
    //     if (sla_time(work) > cur_time) {
    //         max_score += score(work);
    //         cur_time++;
    //     }
    // }
    // cout << max_score;

    // priority_queue<>
    queue<work_type> work_queue;
    for (auto& work: work_vectors) {
        if (sla_time(work) > cur_time) {
            // max_score += score(work);
            // cur_time++;
            work_queue.push(work);
            cur_time++;
        } else if (sla_time(work) == cur_time && (score(work) > score(work_queue.front()))) {
            work_queue.push(work);
            work_queue.pop();
        }
    }
    while (!work_queue.empty()) {
        // work_type& tmp =
        max_score += score(work_queue.front());
        work_queue.pop();
    }
    cout << max_score;
    return 0;
}